package me.monster.smarttraffic.base;

import android.os.Bundle;

import androidx.annotation.Nullable;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-11 14:47
 */
public abstract class AbstractPresenterActivity<P extends IPresenter> extends AbstractToolActivity implements IView {

    public P mPresenter;
    private CompositeDisposable disposables;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = createPresenter();
        mPresenter.attachView(this);
    }

    public void addDispose(Disposable disposable) {
        if (disposables == null) {
            disposables = new CompositeDisposable();
        }
        disposables.add(disposable);
    }

    public void disposable() {
        if (disposables != null) {
            disposables.dispose();
        }
    }

    @Override
    protected void onDestroy() {
        mPresenter.detachView();
        disposable();
        super.onDestroy();
    }

    /**
     * 创建 Presenter
     *
     * @return Presenter
     */
    public abstract P createPresenter();
}
